package map.android.baidu.carpool.animation;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * @author chenlei18
 */
public class AnimationUtilsForCarpool {

    private static AnimationUtilsForCarpool mSingleCarAnimation = null;
    
    private AnimationUtilsForCarpool() {
    }
    
    public synchronized static AnimationUtilsForCarpool getInstance() {
         if (mSingleCarAnimation == null) {    
             mSingleCarAnimation = new AnimationUtilsForCarpool();
         }    
        return mSingleCarAnimation;  
    }  
    
    
    public void initScale(View v){
        AnimationSet set = new AnimationSet(true);
        ScaleAnimation scale = new ScaleAnimation(0.5f, 1f, 0.5f, 1f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(500);

        AlphaAnimation alpha = new AlphaAnimation(0.5f, 1);
        alpha.setDuration(500);
        set.addAnimation(scale);
        set.addAnimation(alpha);
        v.startAnimation(set);
    }
    
    public void translateTopToBottom(View v){
        AnimationSet set = new AnimationSet(true);
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1f,
                Animation.RELATIVE_TO_PARENT, 0);
        trans.setDuration(300);
        set.addAnimation(trans);
        AlphaAnimation alpha = new AlphaAnimation(0.2f, 1f);
        alpha.setDuration(300);
        set.addAnimation(alpha);
        v.startAnimation(set);
    }
    
    public void translateTopToBottomReverse(View v,AnimationListener listener){
        AnimationSet set = new AnimationSet(true);
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1f);
        trans.setDuration(300);
        trans.setAnimationListener(listener);
        set.addAnimation(trans);
        AlphaAnimation alpha = new AlphaAnimation(1f, 0.2f);
        alpha.setDuration(300);
        set.addAnimation(alpha);
        v.startAnimation(set);
    }
    
    public void translateBottomToTop(View v){
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1f,
                Animation.RELATIVE_TO_SELF, 0f);
        trans.setDuration(200);
        v.startAnimation(trans);
    }
    
    public void translateBottomToTopReverse(View v,AnimationListener listener){
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1f);
        trans.setDuration(200);
        trans.setAnimationListener(listener);
        v.startAnimation(trans);
    }
    
    
    
    public void scaleClose(View v,AnimationListener listener){
        AnimationSet set = new AnimationSet(true);
        ScaleAnimation scale = new ScaleAnimation(1f, 0f, 1f, 0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(300);

        AlphaAnimation alpha = new AlphaAnimation(1f, 0.5f);
        alpha.setDuration(300);
        set.addAnimation(scale);
        set.addAnimation(alpha);
        set.setAnimationListener(listener);
        v.startAnimation(set);
    }

}
